package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import com.lb.jwitter.service.ConditionReport;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.util.Message;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import java.util.List;
import java.util.Map;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblRangBuocChuyenNganhCam;
import com.lb.jwitter.service.TblRangBuocChuyenNganhCamService;

import com.lb.jwitter.service.TblCTDTChuyenNganh;
import com.lb.jwitter.service.TblCTDTChuyenNganhService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblRangBuocChuyenNganhCamAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private String idList;

	public String getIdList() {
		return idList;
	}

	public void setIdList(String idList) {
		this.idList = idList;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	private int chuyenNganhId;

	public void setChuyenNganhId(int chuyenNganhId) {
		this.chuyenNganhId = chuyenNganhId;
	}

	public int getChuyenNganhId() {
		return chuyenNganhId;
	}

	private String khoaHoc;

	public void setKhoaHoc(String khoaHoc) {
		this.khoaHoc = khoaHoc;
	}

	public String getKhoaHoc() {
		return khoaHoc;
	}

	private String nhomHoc;

	public void setNhomHoc(String nhomHoc) {
		this.nhomHoc = nhomHoc;
	}

	public String getNhomHoc() {
		return nhomHoc;
	}

	private TblRangBuocChuyenNganhCam tblRangBuocChuyenNganhCam;

	public TblRangBuocChuyenNganhCam getTblRangBuocChuyenNganhCam() {
		return this.tblRangBuocChuyenNganhCam;
	}

	private Collection<TblRangBuocChuyenNganhCam> tblRangBuocChuyenNganhCams;

	public Collection<TblRangBuocChuyenNganhCam> getTblRangBuocChuyenNganhCams() {
		return this.tblRangBuocChuyenNganhCams;
	}

	private TblRangBuocChuyenNganhCamService tblRangBuocChuyenNganhCamService;

	private Collection<TblCTDTChuyenNganh> tblCTDTChuyenNganhs;

	public Collection<TblCTDTChuyenNganh> getTblCTDTChuyenNganhs() {
		return this.tblCTDTChuyenNganhs;
	}

	private TblCTDTChuyenNganhService tblCTDTChuyenNganhService;

	private int preUpdate(Message msg, TblRangBuocChuyenNganhCam obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblRangBuocChuyenNganhCam obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String update() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"sua")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			tblCTDTChuyenNganhService = (TblCTDTChuyenNganhService) context.getBean("tbl_CTDT_ChuyenNganhClient");
			msg = new Message();
			tblRangBuocChuyenNganhCam = tblRangBuocChuyenNganhCamService.gettblRangBuocChuyenNganhCam(new Long(id));
			tblRangBuocChuyenNganhCam.setChuyenNganhId(tblCTDTChuyenNganhService.gettblCTDTChuyenNganh(new Long(chuyenNganhId)));
			tblRangBuocChuyenNganhCam.setKhoaHoc(khoaHoc);
			tblRangBuocChuyenNganhCam.setNhomHoc(nhomHoc);
			preUpdate(msg, tblRangBuocChuyenNganhCam);
			if (msg.isPreMessage_b()) {
				tblRangBuocChuyenNganhCamService
						.updatetblRangBuocChuyenNganhCam(tblRangBuocChuyenNganhCam);
				postUpdate(msg, tblRangBuocChuyenNganhCam);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			tblRangBuocChuyenNganhCams = tblRangBuocChuyenNganhCamService.findAlltblRangBuocChuyenNganhCam(pageSize, page);
			pages = tblRangBuocChuyenNganhCamService.counttblRangBuocChuyenNganhCamByCondition(new ArrayList<ConditionReport>())/ pageSize + 1;
			dataString = "";
			dataString += "&id=";
			dataString += "&chuyenNganhId=";
			dataString += "&khoaHoc=";
			dataString += "&nhomHoc=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("-------------------------------");
			System.out.println("=="+id+"==");
			System.out.println("Loi Update tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			System.out.println("===============================");
			return Action.ERROR;
		}
	}

	public String form() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"xem")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			tblRangBuocChuyenNganhCams = tblRangBuocChuyenNganhCamService.findAlltblRangBuocChuyenNganhCam(pageSize, page);
			pages = tblRangBuocChuyenNganhCamService.counttblRangBuocChuyenNganhCamByCondition(new ArrayList<ConditionReport>())/ pageSize + 1;
			dataString = "";
			dataString += "&id=";
			dataString += "&chuyenNganhId=";
			dataString += "&khoaHoc=";
			dataString += "&nhomHoc=";
			tblCTDTChuyenNganhService = (TblCTDTChuyenNganhService) context
					.getBean("tbl_CTDT_ChuyenNganhClient");
			tblCTDTChuyenNganhs = tblCTDTChuyenNganhService
					.findAlltblCTDTChuyenNganh(0, 0);
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Form tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	public String delete() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"xoa")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context
					.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			msg = new Message();

			List<String> ids = new ArrayList<String>();
			Collections.addAll(ids, idList.split(","));
			for (String id : ids) {
				tblRangBuocChuyenNganhCamService
						.deleteListtblRangBuocChuyenNganhCam(ids);
			}
			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			tblRangBuocChuyenNganhCams = tblRangBuocChuyenNganhCamService
					.findAlltblRangBuocChuyenNganhCam(pageSize, page);
			pages = tblRangBuocChuyenNganhCamService
					.counttblRangBuocChuyenNganhCamByCondition(new ArrayList<ConditionReport>())
					/ pageSize + 1;
			dataString = "";
			dataString += "&id=";
			dataString += "&chuyenNganhId=";
			dataString += "&khoaHoc=";
			dataString += "&nhomHoc=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out
					.println("Loi Delete tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	private int preInsert(Message msg, TblRangBuocChuyenNganhCam obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblRangBuocChuyenNganhCam obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	public String insert() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"them")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context
					.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			tblCTDTChuyenNganhService = (TblCTDTChuyenNganhService) context
					.getBean("tbl_CTDT_ChuyenNganhClient");
			msg = new Message();
			tblRangBuocChuyenNganhCam = new TblRangBuocChuyenNganhCam();
			tblRangBuocChuyenNganhCam
					.setChuyenNganhId(tblCTDTChuyenNganhService
							.gettblCTDTChuyenNganh(new Long(chuyenNganhId)));
			tblRangBuocChuyenNganhCam.setKhoaHoc(khoaHoc);
			tblRangBuocChuyenNganhCam.setNhomHoc(nhomHoc);
			preInsert(msg, tblRangBuocChuyenNganhCam);
			if (msg.isPreMessage_b()) {
				id = tblRangBuocChuyenNganhCamService
						.createtblRangBuocChuyenNganhCam(tblRangBuocChuyenNganhCam);
				postInsert(msg, tblRangBuocChuyenNganhCam);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			tblRangBuocChuyenNganhCam.setId(id);
			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			tblRangBuocChuyenNganhCams = tblRangBuocChuyenNganhCamService
					.findAlltblRangBuocChuyenNganhCam(pageSize, page);
			pages = tblRangBuocChuyenNganhCamService
					.counttblRangBuocChuyenNganhCamByCondition(new ArrayList<ConditionReport>())
					/ pageSize + 1;
			dataString = "";
			dataString += "&id=";
			dataString += "&chuyenNganhId=";
			dataString += "&khoaHoc=";
			dataString += "&nhomHoc=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out
					.println("Loi Insert tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	private int preSearch(Message msg, List<ConditionReport> cons) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postSearch(Message msg, List<ConditionReport> cons) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	public String search() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"xem")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context
					.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			msg = new Message();
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			if (id != null && !id.equals(0)) {
				ConditionReport con = new ConditionReport();
				con.setCol("id");
				con.setValue1(new Long(id));
				con.setOperator("eq");
				cons.add(con);
			}
			if (chuyenNganhId != 0) {
				ConditionReport con = new ConditionReport();
				con.setCol("chuyenNganhId.id");
				con.setValue1(new Long(chuyenNganhId));
				con.setOperator("eq");
				cons.add(con);
			}
			if (khoaHoc != null && !khoaHoc.equalsIgnoreCase("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("khoaHoc");
				con.setValue1("%" + khoaHoc + "%");
				con.setOperator("like");
				cons.add(con);
			}
			if (nhomHoc != null && !nhomHoc.equalsIgnoreCase("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("nhomHoc");
				con.setValue1("%" + nhomHoc + "%");
				con.setOperator("like");
				cons.add(con);
			}
			preSearch(msg, cons);
			if (msg.isPreMessage_b()) {
				if (pageSize == 0) {
					pageSize = 20;
				}
				if (page == 0) {
					page = 1;
				}
				tblRangBuocChuyenNganhCams = tblRangBuocChuyenNganhCamService
						.findtblRangBuocChuyenNganhCamByCondition(cons,
								pageSize, page);
				pages = tblRangBuocChuyenNganhCamService
						.counttblRangBuocChuyenNganhCamByCondition(cons)
						/ pageSize + 1;
				postSearch(msg, cons);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			dataString = "";
			dataString += "&id=" + id;
			dataString += "&chuyenNganhId=" + chuyenNganhId;
			dataString += "&khoaHoc=" + khoaHoc;
			dataString += "&nhomHoc=" + nhomHoc;
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Seach tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	public String editForm() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_RangBuoc_ChuyenNganh_Cam",
					"sua")) {
				return "noprivilege";
			}
			tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context
					.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
			tblRangBuocChuyenNganhCam = tblRangBuocChuyenNganhCamService
					.gettblRangBuocChuyenNganhCam(new Long(id));
			tblCTDTChuyenNganhService = (TblCTDTChuyenNganhService) context
					.getBean("tbl_CTDT_ChuyenNganhClient");
			tblCTDTChuyenNganhs = tblCTDTChuyenNganhService
					.findAlltblCTDTChuyenNganh(0, 0);

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out
					.println("Loi EditForm tbl_RangBuoc_ChuyenNganh_Cam Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
